package cn.diancan.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Random;


public class ReadTxtFile {

 
	public static String readTxtFile(String filePath) {
		String allTxt = "";
		try {
			String encoding = "UTF-8";
			File file = new File(filePath);
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				InputStreamReader read = new InputStreamReader(
						new FileInputStream(file), encoding);// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					allTxt += lineTxt.trim();
				}
				read.close();
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}
		return allTxt;
	}
	
	public static String readTxtFile(String filePath,String charSet) {
		String allTxt = "";
		try {
			String encoding = charSet;
			File file = new File(filePath);
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				InputStreamReader read = new InputStreamReader(
						new FileInputStream(file), encoding);// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					allTxt += lineTxt.trim();
				}
				read.close();
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}
		return allTxt;
	}
	
	
	public static File writeXmlFile(InputStream in,String outfilepath) {
		 
		
		File tmp = new File(outfilepath);
		try {
			tmp = new File(outfilepath);
		if (!tmp.exists()) {
			
			//创建文件
			tmp.createNewFile();
		}
		FileOutputStream out = new FileOutputStream(tmp);
		int b1 = 0;

	 	while ((b1 = in.read( )) != -1) {
			out.write(b1);
		}
		in.close();
		out.flush();
		out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} 
		return tmp;
	}
	
	
	
	public static File writeXmlFile(InputStream in,String path,String outfilepath) {
		try {//tmp.mkdirs();//创建目录
			File tmp2 = new File(path);
			if (!tmp2.exists()) {
				tmp2.mkdirs();
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		File tmp = new File(outfilepath);
		try {
			tmp = new File(outfilepath);
		if (!tmp.exists()) {
			
			//创建文件
			tmp.createNewFile();
		}
		FileOutputStream out = new FileOutputStream(tmp);
		int b1 = 0;

	 	while ((b1 = in.read( )) != -1) {
			out.write(b1);
		}
		in.close();
		out.flush();
		out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} 
		return tmp;
	}
	
	
	public static String readTxtFile(InputStream in) {
		String allTxt = "";
		try {
				String encoding = "UTF-8";
				InputStreamReader read = new InputStreamReader(in, encoding);// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					allTxt += lineTxt.trim();
				}
				read.close();
			
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}
		return allTxt;
	}
	

	public static void main(String argv[]) {
		String filePath = "E:/ProgramFiles/workspace/tongcheng/src/OTA_HotelRes.xml";
	 
		System.out.println(readTxtFile(filePath));
	}
}